﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PersonnelManagement.Infrstructre
{
    public class BaseRepository<T> : IRepository<T> where T : class, new()
    {
        ISqlSugarClient db;
        public BaseRepository(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Add(T entity)
        {
            return await db.Insertable(entity).ExecuteCommandAsync();
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> DelRange(T entity)
        {
            return  db.Deleteable(entity).ExecuteCommand();
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>

        public async Task<ISugarQueryable<T>> Getall()
        {
            return  db.Queryable<T>();
        }
        /// <summary>
        /// 没有异步获取所有数据
        /// </summary>
        /// <returns></returns>
        public List<T> Quesry()
        {
           return db.Queryable<T>().ToList();
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Task<int> UpdateAsymc(T entity)
        {
            return db.Updateable(entity).ExecuteCommandAsync();
        }
    }
}
